var result=true;

var divUserRegister = new Vue({
    el: '#hl-body',
    data: function() {
        var self = this;
        /**
         * 用户名复杂度校验.
         * @param rule
         * @param value
         * @param callback
         * @returns {*}
         */
        var checkUser = function (rule, value, callback) {
            if (!CndnsValidate.checkUserName(value)) {
                return callback(new Error('只能包含字母或数字，不能纯数字'));
            }
            callback();
        };

        /**
         * 密码复杂度校验.
         * @param rule
         * @param value
         * @param callback
         * @returns {*}
         */
        var checkPassword = function (rule, value, callback) {
            if (!CndnsValidate.checkPassWord(value)) {
                return callback(new Error('密码应由大写字母+小写字母+数字或者字母+数字+符号组成'));
            }
            callback();
        };

        /**
         * 密码确认检查.
         * @param rule
         * @param value
         * @param callback
         * @returns {*}
         */
        var checkPasswordConfirm = function (rule, value, callback) {
            if (value != self.form.password) {
                return callback(new Error('确认输入的密码不一致'));
            }
            callback();
        };

        /**
         * 检查手机号码.
         * @param rule
         * @param value
         * @param callback
         */
        var checkMobile = function (rule, value, callback) {
            if (!CndnsValidate.checkMobile(value)) {
                return callback(new Error('请确认输入的手机号码是否正确'));
            }
            callback();
        };

        /**
         * 检查邮箱.
         * @param rule
         * @param value
         * @param callback
         * @returns {*}
         */
        var checkEmail = function (rule, value, callback) {
            if (!CndnsValidate.checkEmail(value)) {
                return callback(new Error('请确认输入的邮箱是否正确'));
            }
            callback();
        };

        return {
            title: '',
            slogan: '',
            show: true,  // 初始启用按钮
            count: '',   // 初始化次数
            timer: null,
            paramList:[],
            validateWay:'sms',
            imgValidate:false,
            form: {
                userType: 0,
                userName: '',
                password: '',
                passwordConfirm: '',
                checkcode: '',
                verificationCodeMobile:'',
                verificationCodeMail:'',
                email: '',
                mobile: ''
            },
            rules: {
            },
            userSet: {
                passLevel: '1',
                forbid: '',
                randCode: true,
                registerItem: [
                    {
                        name: '邮箱',
                        code: 'email',
                        show: true,
                        must: true,
                        auth: false
                    },
                    {
                        name: '手机号',
                        code: 'mobile',
                        show: true,
                        must: false,
                        auth: false
                    }
                ]
            },
            userSetEmail: {
                name: '邮箱',
                code: 'email',
                show: true,
                must: true
            },
            userSetMobile: {
                name: '手机号',
                code: 'mobile',
                show: true,
                must: false
            },
            showMail: true,
            countMail: '',   // 初始化次数
            timerMail: null
        }
    },
    methods: {
        /**
         * 用户注册.
         */
        register: function () {
            var self = this;
            this.$refs['form'].validate(function (valid) {
                if (valid) {
                    var service = self.form;


                    var fn = "registerV2";
                    service = Commonjs.jsonToString(service)
                    var params = Commonjs.getParams(fn,service);


                    //请求数据.
                    winiisRequest.get(weburl, params, function (data) {
                        if(data==null){
                            return false;
                        }
                        self.$message.success("账号已注册成功。");
                        location.href = '/default/user-login.html';

                    }, function (data) {
                        self.$message.error(data.msg);
                    });

                } else {
                    return false;
                }
            });
        },
        getLoginWay(){
            var self = this;
            var paramEName="codeLoginWay";
            var service = {};
            var fn = "getparam";
            service.code = paramEName;
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, service);//获取参数
            Commonjs.ajaxTrue(sysurl,params,function (data) {
                if(data.data.length > 0){
                   self.validateWay = data.data[0].paramValue;
                }
            },false);

            paramEName="globalOperationUserRegister";
            fn = "getparam";
            service = {};
            service.code = paramEName;
            service = Commonjs.jsonToString(service);
            params = Commonjs.getParams(fn, service);//获取参数
            Commonjs.ajaxTrue(sysurl,params,function (data) {
                if (data.data && data.data.length > 0) {
                    self.userSet = data.data[0].paramValue;
                    self.userSet.registerItem.forEach(function (value) {
                        if (value.code=='email') {
                            self.userSetEmail = value;
                        } else if (value.code == 'mobile') {
                            self.userSetMobile = value;
                        }
                    });
                }
            },false);
        },
        getRegVeifyrImg(){
            var self = this;
            var paramEName="regVerifyImage";
            var service = {};
            var fn = "getparam";
            service.code = paramEName;
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, service);//获取参数
            Commonjs.ajaxTrue(sysurl,params,function (data) {
                if(data.data.length > 0){
                   if(data.data[0].paramValue == 2){
                        self.imgValidate = true;
                   }else{
                        self.imgValidate = false;
                   }
                }
            },false);
        },
        getRegisterCode(){
            var self = this;
            if (!this.timer) {
                if(this.sendRegisterCode()){
                    this.count = 60;
                    this.show = false;
                    this.timer = setInterval(function (){
                        if (self.count > 0 && self.count <= 60) {
                            self.count--;
                        } else {
                            self.show = true;
                            clearInterval(self.timer);  // 清除定时器
                            self.timer = null;
                        }
                    }, 1000);
                }

            }
        },
        sendRegisterCode(){
            var self = this;
            var service = {};
            var fn = "getRegisterCodeNew";

            var mobile = $("#txtMobile").val();
            service.userName = mobile;
            service.type = 'mobile';
            if(Commonjs.isEmpty(service.userName)){
                self.$message.error("手机号不能为空！");
                return false;
            }

            if (!this.timer) {
                this.count = 60;
                this.show = false;
                this.timer = setInterval(function () {
                    if (self.count > 0 && self.count <= 60) {
                        self.count--;
                    } else {
                        self.show = true;
                        clearInterval(self.timer);  // 清除定时器
                        self.timer = null;
                    }
                }, 1000);
            }
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, service);//获取参数
            Commonjs.ajaxTrue(weburl, params, function (data) {
                if(data==null){
                    return false;
                }
            }, function (data) {
                self.$message.error(data.msg);
                clearInterval(self.timer);
                self.timer = null;
                self.show = true;
            });
        },
        /**
         * 发送邮箱验证码.
         * @returns {boolean}
         */
        sendRegisterCodeMail: function () {
            var self = this;
            var service = {};
            var fn = "getRegisterCodeNew";
            service.userName = $.trim($("#txtEmail").val());
            service.type = 'mail';
            if(Commonjs.isEmpty(service.userName)){
                self.$message.error("邮箱账号不能为空！");
                return false;
            }
            if (!this.timerMail) {
                this.countMail = 60;
                this.showMail = false;
                this.timerMail = setInterval(function () {
                    if (self.countMail > 0 && self.countMail <= 60) {
                        self.countMail--;
                    } else {
                        self.showMail = true;
                        clearInterval(self.timerMail);  // 清除定时器
                        self.timerMail = null;
                    }
                }, 1000)
            }
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, service);//获取参数
            winiisRequest.get(weburl, params, function (data) {
                if(data==null){
                    return false;
                }

            }, function (data) {
                self.$message.error(data.msg);
                clearInterval(self.timerMail);
                self.timerMail = null;
                self.showMail = true;
            });
        }
    },
    mounted: function () {
        //加截验证码
        refreshVcode();
        this.getLoginWay();
        this.getRegVeifyrImg();
        // this.sendRegisterCode();
    }
});
$(function () {
    //会员名
    var comserver = '《'+ webname +'用户协议》';
    $('#comserver').html(comserver);
    $('.register_shoppingcart_center2_201').html(webname);
    $("#txtUserName").blur(function () {
        var userName = $(this).val();
        var objthis = $(this);
        var obj = $(this).parent().parent().next("li");
        userName = userName.toLowerCase();
        objthis.parent().removeClass('on1').removeClass('on2');
        if ($.trim(userName) == "") {
            objthis.parent().addClass('on1');
            objthis.parent().parent().find("p").hide();
            obj.attr("class", "register_msg msg1 dh");
            obj.text("会员名不能为空");
            return;
        }
        if(!CndnsValidate.checkUserName(userName)){
        	objthis.parent().addClass('on1');
            objthis.parent().parent().find("p").hide();
            obj.attr("class", "register_msg msg1 dh");
            obj.text("会员名不符合规则");
            result= false;
        }else {
            obj.attr("class", "register_msg msg3");
            obj.text("");
        }
        isRegister(userName);
    });

    //会员密码
    $("#txtPassWord").blur(function () {
        var usrPassWord = $(this).val();
        var obj = $(this).parent().parent().next("li");

        $(this).parent().removeClass('on1').removeClass('on2');

        var checkResult = true;
        if (Commonjs.isEmpty(usrPassWord)) {
            checkResult = false;
        } else {
            if (divUserRegister.userSet.passLevel == 1 && !CndnsValidate.checkPassWord(usrPassWord)) {
                checkResult = false;
            } else if (!CndnsValidate.checkPasswordLevel2(usrPassWord)) {
                checkResult = false;
            }
        }

        if (!checkResult) {
            $(this).parent().addClass('on1');
            obj.attr("class", "register_msg msg1 dh");
            obj.text("密码不符合要求");
            obj.show();
            $(this).parent().parent().find("p").hide();
            result= false;
        } else {
            obj.attr("class", "register_msg msg3");
            obj.text("");
        }
    });

    $("#txtPassConfirm").blur(function () {
        var usrPassConfirm = $(this).val();
        var usrPassWord = $("#txtPassWord").val();
        var obj = $(this).parent().parent().next("li");
        $(this).parent().removeClass('on1').removeClass('on2');
        if ($.trim(usrPassConfirm) == "" || usrPassConfirm != usrPassWord) {
            $(this).parent().addClass('on1');
            obj.attr("class", "register_msg msg1 dh");
            obj.text("两次密码输入不一致，请重新输入");
            result= false;
        } else {
            obj.attr("class", "register_msg msg3");
            obj.text("");
        }
    });

    //邮箱
    $("#txtEmail").blur(function () {
        var Email = $(this).val();
        var objthis = $(this);
        var obj = $(this).parent().parent().next("li");
        Email = Email.toLowerCase();
        objthis.parent().removeClass('on1').removeClass('on2');

        if (!divUserRegister.userSetEmail.must) {
            return;
        }

        if ($.trim(Email) == "") {
            objthis.parent().addClass('on1');
            objthis.parent().parent().find("p").hide();
            obj.attr("class", "register_msg msg1 dh");
            obj.text("邮箱不能为空");
            return;
        }
        if (!CndnsValidate.checkEmail(Email)) {
            objthis.parent().addClass('on1');
            objthis.parent().parent().find("p").hide();
            obj.attr("class", "register_msg msg1 dh");
            obj.text("邮箱格式错误");
            result= false;
        }else{
        	obj.attr("class", "register_msg msg3");
        	obj.text("");
            isRegisterEmail(Email);
        }
    });

    //手机号
    $("#txtMobile").blur(function () {
        if (!divUserRegister.userSetMobile.must) {
            return;
        }

        var usrMbl = $(this).val();
        var obj = $(this).parent().parent().next("li");
        $(this).parent().removeClass('on1').removeClass('on2');
        if ($.trim(usrMbl) == "" || !CndnsValidate.checkMobile(usrMbl)) {
            $(this).parent().addClass('on1');
            obj.attr("class", "register_msg msg1 dh");
            obj.text("请填写真实手机号码");
            result= false;
        } else {
            obj.attr("class", "register_msg msg3");
            obj.text("");
        }
    });

    $("#registercode").blur(function () {
        var val = $(this).val();
        if (val.length != 4) {
            $("#codemsg").attr("class", "register_msg msg1 dh");
            $("#codemsg").text("请填写4位验证码!");
            result= false;
        }
    });
});

function isRegister(userName){
	var service = {};
	service.userName = userName;
	var fn="isRegister";
	service = Commonjs.jsonToString(service)
	var params = Commonjs.getParams(fn,service);//获取参数
	Commonjs.ajaxTrue(weburl,params,isRegisterSuccess,true,"正在检测中...");
}

function isRegisterSuccess(data){
	if(data.data==null)
		return;

	if(data.data.code=="-1"){
		//Commonjs.alert("该会员名已注册！");
		var objthis = $("#txtUserName");
		var obj = objthis.parent().parent().next("li");
	    objthis.parent().removeClass('on1').removeClass('on2');
		objthis.parent().addClass('on1');
        objthis.parent().parent().find("p").hide();
        obj.attr("class", "register_msg msg1 dh");
        obj.text("该会员名已注册");
	}
}

function isRegisterEmail(Email){
	var service = {};
	service.Email = Email;
	var fn="isRegisterEmail";
	service = Commonjs.jsonToString(service)
	var params = Commonjs.getParams(fn,service);//获取参数
	Commonjs.ajaxTrue(weburl,params,isRegisterEmailSuccess,true,"正在检测中...");
}

function isRegisterEmailSuccess(data){
	if(data.data==null)
		return;

	if(data.data.code=="-1"){
		//Commonjs.alert("该会员名已注册！");
		var objthis = $("#txtEmail");
		var obj = objthis.parent().parent().next("li");
	    objthis.parent().removeClass('on1').removeClass('on2');
		objthis.parent().addClass('on1');
        objthis.parent().parent().find("p").hide();
        obj.attr("class", "register_msg msg1 dh");
        obj.text("该邮箱已注册");
	}
}

//注册会员
function regMember(objs) {
    $(objs).next('p').html('');
    var usrName = $("#txtUserName").val();
    var usrPassWord = encodeURIComponent($("#txtPassWord").val());
    var usrPassConfirm = encodeURIComponent($("#txtPassConfirm").val());
    var usrMbl = $("#txtMobile").val();
    var user_u_ctt_cn = $("#agreeBox").val();
    var verificationCodeMobile = $.trim(divUserRegister.form.verificationCodeMobile);
    var verificationCodeMail = $.trim(divUserRegister.form.verificationCodeMail);
    var code = $("#registercode").val();
	var email = $("#txtEmail").val();
    var obj = "";
    var this_obj = "";
    usrName = usrName.toLowerCase();
	 if ($.trim(usrName) == "" || !CndnsValidate.checkUserName(usrName)) {
        $("#txtUserName").parent().removeClass('on1').removeClass('on2').addClass('on1');
        $("#txtUserName").parent().parent().find("p").hide();
        $("#txtUserName").parent().parent().next("li").attr("class", "register_msg msg1 dh");
        $("#txtUserName").parent().parent().next("li").text("会员名格式错误");
		$("#txtUserName").focus();
        return false;
    }

    var checkPassResult = true;
	 if ($.trim(usrPassWord) == "") {
	     checkPassResult = false;
     } else {
         if (Commonjs.isEmpty(usrPassWord)) {
             checkPassResult = false;
         } else {
             if (divUserRegister.userSet.passLevel == 1 && !CndnsValidate.checkPassWord(usrPassWord)) {
                 checkPassResult = false;
             } else if (!CndnsValidate.checkPasswordLevel2(usrPassWord)) {
                 checkPassResult = false;
             }
         }
     }
    if (!checkPassResult) {
        obj = $("#txtPassWord");
        this_obj = obj.parent().parent().next("li");
        obj.parent().removeClass('on1').removeClass('on2').addClass('on1');
        this_obj.attr("class", "register_msg msg1 dh");
        this_obj.text("密码不符合要求");
		obj.focus();
        return false;
    }
    if ($.trim(usrPassConfirm) == "" || usrPassConfirm != usrPassWord) {
        obj = $("#txtPassConfirm");
        this_obj = obj.parent().parent().next("li");
        obj.parent().removeClass('on1').removeClass('on2').addClass('on1');
        this_obj.attr("class", "register_msg msg1 dh");
        this_obj.text("两次密码输入不一致，请重新输入");
		obj.focus();
        return false;
    }

    if (divUserRegister.userSetEmail.must || divUserRegister.userSetEmail.auth) {
	     if ($.trim(email)=="" || !CndnsValidate.checkEmail(email)) {
	         obj = $('#txtEmail');
             this_obj = obj.parent().parent().next("li");
             this_obj.parent().addClass('on1');
             this_obj.parent().parent().find("p").hide();
             obj.attr("class", "register_msg msg1 dh");
             obj.text("邮箱格式错误");
             return false;
         }
         if (divUserRegister.userSetEmail.auth && Commonjs.isEmpty(verificationCodeMail)) {
	         divUserRegister.$message.error('请输入邮箱认证码');
	         return false;
         }
    }
    if (!Commonjs.isEmpty(email) && !CndnsValidate.checkEmail(email)) {
        obj = $('#txtEmail');
        this_obj = obj.parent().parent().next("li");
        this_obj.parent().addClass('on1');
        this_obj.parent().parent().find("p").hide();
        obj.attr("class", "register_msg msg1 dh");
        obj.text("邮箱格式错误");
        return false;
    }

    if (divUserRegister.userSetMobile.must || divUserRegister.userSetMobile.auth) {
	     if ($.trim(usrMbl) == "" || !CndnsValidate.checkMobile(usrMbl)) {
             obj = $('#txtMobile');
             this_obj = obj.parent().parent().next("li");
             this_obj.parent().addClass('on1');
             this_obj.parent().parent().find("p").hide();
             obj.attr("class", "register_msg msg1 dh");
             obj.text("手机号格式错误");
             return false;
         }
         if (divUserRegister.userSetMobile.auth && Commonjs.isEmpty(verificationCodeMobile)) {
	         divUserRegister.$message.error('请输入手机认证码');
	         return false;
         }
    }
    if (!Commonjs.isEmpty(usrMbl) && !CndnsValidate.checkMobile(usrMbl)) {
        obj = $('#txtMobile');
        this_obj = obj.parent().parent().next("li");
        this_obj.parent().addClass('on1');
        this_obj.parent().parent().find("p").hide();
        obj.attr("class", "register_msg msg1 dh");
        obj.text("手机号格式错误");
        return false;
    }

    if (divUserRegister.userSet.randCode && $.trim(code) == "" && divUserRegister.imgValidate) {
    	obj = $("#registercode");
        var obj = obj.parent().parent().next("li");
        $(this).parent().removeClass('on1').removeClass('on2');
        if (code.length != 4) {
            $(this).parent().addClass('on1');
            obj.attr("class", "register_msg msg1 dh");
            obj.text("请填写4位验证码!");
			obj.focus();
            return false;
        }
    }

    var service = {};
	service.userName = usrName;
	service.userType = $('input[name="userType"]:checked').val();
    if(divUserRegister.validateWay=='mail'){
        service.email = divUserRegister.form.email;
    }else{
        service.mobile = divUserRegister.form.mobile;
    }
	service.email = email;
	service.mobile = usrMbl;
	service.password=usrPassWord;
	service.checkcode=code;
    service.verificationCodeMail = verificationCodeMail;
    service.verificationCodeMobile = verificationCodeMobile;
	var fn="registerV2";
	service = Commonjs.jsonToString(service)
	var params = Commonjs.getParams(fn,service);//获取参数
	Commonjs.ajaxTrue(weburl,params,registerSuccess,true,"正在注册中...");
}

function registerSuccess(data) {
	if(data==null){
		return false;
	}
    divUserRegister.$message({
        showClose: true,
        message: '账号已注册成功，请前往登录',
        type: 'success',
        onClose: function () {
            location.href = '/default/login.html';
        }
    });
}
//获取验证码
refreshVcode();
